﻿using LuShanTea.DTO;
using LuShanTea.IService;
using LuShanTea.Service.Entities;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LuShanTea.Service
{
    public class OrderRecordService : IOrderRecordService
    {
        /// <summary>
        /// 添加订单纪录
        /// </summary>
        /// <param name="orderRecordAddNewDTO"></param>
        /// <returns></returns>
        public long AddNew(OrderRecordAddNewDTO orderRecordAddNewDTO)
        {
            using (LuShanTeaContext context = new LuShanTeaContext())
            {
                OrderRecord orderRecord = new OrderRecord()
                {
                    Count = orderRecordAddNewDTO.Count,
                    GoodId = orderRecordAddNewDTO.GoodId,
                    OrderId = orderRecordAddNewDTO.OrderId,
                    PackId = orderRecordAddNewDTO.PackId,
                    Price = orderRecordAddNewDTO.Price
                };

                context.OrderRecords.Add(orderRecord);

                context.SaveChanges();

                return orderRecord.Id;
            }
        }

        /// <summary>
        /// 根据订单Id获取订单纪录
        /// </summary>
        /// <param name="orderId">订单Id</param>
        /// <returns></returns>
        public OrderRecordDTO[] GetOrderRecordByOrderId(long orderId)
        {
            using (LuShanTeaContext context = new LuShanTeaContext())
            {
                BaseService<OrderRecord> baseService = new BaseService<OrderRecord>(context);

                var orderRecords = baseService.GetAll()
                    .Where(o => o.OrderId == orderId)
                    .Include(o => o.Good).Include(o => o.Order)
                    .Include(o => o.Pack).Include(o => o.Comment).AsNoTracking();

                return orderRecords.ToList().Select(o => ToDTO(o)).ToArray();
            }
        }

        /// <summary>
        /// 软删除订单纪录数据
        /// </summary>
        /// <param name="orderRecordId">订单纪录Id</param>
        public void MarkDeleted(long orderRecordId)
        {
            using (LuShanTeaContext context = new LuShanTeaContext())
            {
                BaseService<OrderRecord> baseService = new BaseService<OrderRecord>(context);

                OrderRecord orderRecord = baseService.GetById(orderRecordId);

                if (orderRecord == null)
                {
                    throw new ArgumentException(string.Format($"订单Id为：{orderRecordId}不存在"));
                }

                baseService.MakeDelete(orderRecordId);
            }
        }

        public OrderRecordDTO GetById(long orderRecordId)
        {
            using (LuShanTeaContext context = new LuShanTeaContext())
            {
                BaseService<OrderRecord> baseService = new BaseService<OrderRecord>(context);

                OrderRecord orderRecord = baseService.GetAll()
                    .Include(o => o.Good).Include(o => o.Order)
                    .Include(o => o.Pack).Include(o => o.Comment)
                                    .SingleOrDefault(o => o.Id == orderRecordId);

                return orderRecord == null ? null : ToDTO(orderRecord);
            }
        }



        /// <summary>
        /// 更新订单纪录数据
        /// </summary>
        /// <param name="orderRecordDTO"></param>
        public void UpdateOrderRecord(OrderRecordDTO orderRecordDTO)
        {
            using (LuShanTeaContext context = new LuShanTeaContext())
            {
                BaseService<OrderRecord> baseService = new BaseService<OrderRecord>(context);
                var orderRecord = baseService.GetById(orderRecordDTO.Id);

                if (orderRecord == null)
                {
                    throw new ArgumentException("没有找到ID为：" + orderRecordDTO.Id + "的订单纪录");
                }

                orderRecord.CommentId = orderRecordDTO.CommentId;
                orderRecord.Count = orderRecordDTO.Count;
                orderRecord.GoodId = orderRecordDTO.GoodId;
                orderRecord.OrderId = orderRecordDTO.OrderId;
                orderRecord.PackId = orderRecordDTO.PackId;
                orderRecord.Price = orderRecordDTO.Price;

                context.SaveChanges();
            }
        }

        private OrderRecordDTO ToDTO(OrderRecord orderRecord)
        {
            OrderRecordDTO orderRecor = new OrderRecordDTO();

            orderRecor.CommentId = orderRecord.CommentId;
            orderRecor.Count = orderRecord.Count;
            orderRecor.GoodId = orderRecord.GoodId;
            orderRecor.GoodName = orderRecord.Good.Name;
            orderRecor.GoodPrice = orderRecord.Good.Price;
            orderRecor.Id = orderRecord.Id;
            orderRecor.CreateDateTime = orderRecord.CreateDateTime;
            orderRecor.Level = orderRecord.Comment == null ? 0 : orderRecord.Comment.Level;
            orderRecor.OrderGuid = orderRecord.Order.Guid;
            orderRecor.OrderId = orderRecord.OrderId;
            orderRecor.Price = orderRecord.Price;
            orderRecor.PackId = orderRecord.PackId;
            orderRecor.PackName = orderRecord.Pack.Name;
            orderRecor.PackPric = orderRecord.Pack.Price;
            return orderRecor;
        }

        public bool CheckUserIsOrderRecord(long userId, long orderStatusId, long orderRecordId)
        {
            using (LuShanTeaContext context = new LuShanTeaContext())
            {
                BaseService<OrderRecord> orderRecordBs = new BaseService<OrderRecord>(context);
                var orderRecord = orderRecordBs.GetById(orderRecordId);
                if (orderRecord == null)
                {
                    throw new ArgumentException("订单记录id不存在");
                }
                BaseService<Order> orderBs = new BaseService<Order>(context);
                var orders = orderBs.GetAll().Where(e => e.UserId == userId && e.StatusId == orderStatusId).ToList();
                if (orders == null)
                {
                    return false;
                }
                return orders.Any(e => e.Id == orderRecord.OrderId);
            }

        }

        public OrderRecordDTO[] GetORPackIdByCommentIds(IEnumerable<long> commentIds)
        {
            using (LuShanTeaContext context = new LuShanTeaContext())
            {
                BaseService<OrderRecord> bs = new BaseService<OrderRecord>(context);
                var orderRecords = bs.GetAll();
                List<OrderRecord> list = new List<OrderRecord>();

                foreach (var commentId in commentIds)
                {
                    var orderRecord = orderRecords
                        .Include(o => o.Good)
                        .Include(o => o.Order)
                        .Include(o => o.Pack)
                        .Include(o => o.Comment)
                        .AsNoTracking().SingleOrDefault(e => e.CommentId == commentId);
                    if (orderRecord==null)
                    {
                        throw new ArgumentException("找不到该订单");
                    }
                    list.Add(orderRecord);
                }
                return list.Select(e=>ToDTO(e)).ToArray();
            }
        }
    }
}
